package swings.panels;

import entity.CommonRecord;
import swings.labels.BgLabel;
import swings.labels.RecordLabel;
import utils.DataManageUtils;

import java.awt.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class RecordPanel extends CommonPanel{
    
    public static int x = 5;
    public static int y = 48;
    public static int width = 290;
    public static int height = 707;
    public static BgLabel bg = new BgLabel(x,y,width,height);
    private WorkPanel workPanel;
    public Map<Integer, RecordLabel> recordMap = new HashMap<>();
    public Boolean isMultipleChoice = false;
    public int chosenId = 0;
    public Map<Integer, Integer> multipleChoiceMap = new HashMap<>();

    public RecordPanel(WorkPanel workPanel) throws IOException, SQLException, ClassNotFoundException {
        this.workPanel = workPanel;
        initCommponts();
        initRecords("");
    }

    private void initCommponts(){
//        this.add(new LineLabel(0,45,width,5));
        this.setBounds(x,y,width,height);
        this.setPreferredSize(new Dimension(width,31*35+15));
    }

    public void initRecords(String data) throws IOException, SQLException, ClassNotFoundException {
        Map<String,Object> valueMap = new HashMap<>();
        valueMap.put("data", data);
        removeAll();
        int yLabel = 5;
        Connection connection = DataManageUtils.getConnection();
        Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet resultSet = DataManageUtils.getAllRecords(statement, valueMap);
        //结果集指针指到最后一行数据，用于获取结果总数
        resultSet.last();
        int resultSetLength = resultSet.getRow();
        //结果集指针指到开头
        resultSet.beforeFirst();
        //遍历结果集并将数据存入数组
        for (int m=0;m<resultSetLength;m++){
            CommonRecord commonRecord = new CommonRecord();
            resultSet.next();
            commonRecord.setId(resultSet.getInt("id"));
            commonRecord.setTitle(resultSet.getString("title"));
            commonRecord.setUrl(resultSet.getString("url"));
            commonRecord.setMethod(resultSet.getString("method"));
            commonRecord.setIsEncrypt(resultSet.getString("isEncrypt"));
            commonRecord.setDataSecret(resultSet.getString("dataSecret"));
            commonRecord.setDataSecretIV(resultSet.getString("dataSecretIV"));
            commonRecord.setAppSecret(resultSet.getString("appSecret"));
            commonRecord.setBody(resultSet.getString("body"));
            commonRecord.setResult(resultSet.getString("result"));
            RecordLabel cache = new RecordLabel(commonRecord.getId(), yLabel, commonRecord, workPanel,this);
            recordMap.put(commonRecord.getId(), cache);
            this.add(cache);
            yLabel += 35;
        }
        // 关闭链接
        resultSet.close();
        statement.close();
        connection.close();
        this.setPreferredSize(new Dimension(width,31*resultSetLength+15));
        refresh();
    }

    public void refreshRecordList(){
        Set<Map.Entry<Integer, RecordLabel>> entries = recordMap.entrySet();
        for (Map.Entry<Integer, RecordLabel> entry : entries) {
            RecordLabel cache = entry.getValue();
            if (cache.id != chosenId){
                cache.removeIsChoose();
                cache.isChoose = false;
            }
        }
        refresh();
    }

    public void refresh(){
        invalidate();
        validate();
        repaint();
    }

}
